Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

285
Vistas
[INQUIRY ]JavaScript - Variable Initilization

function calculateArea(r){
  var area;
  if (r<=0){
    return 0;
  } else {
    area=Math.PI*r*r; 
    return area; 
  }
}

var radius = 5.2; 
var theArea=calculateArea(radius);
console.log("Area = " + theArea); 

New here, please let me know if I have posted incorrectly.

If the initialization of a variable is a function. Does the function execute automatically? For instance, see my code below. I get the desired output in the console log. Had I not included this function in the variable, would it have still ran?

   function calculateArea(r){
  var area;
  if (r<=0){
    return 0;
  } else {
    area=Math.PI*r*r; 
    return area; 
  }
}

var radius = 5.2; 
var theArea=calculateArea(radius);
console.log("Area = " + theArea); 

EDIT: Think I've got my head wrapped around this not. Thank you all!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

if you call it without any arguments

calculateArea()

r will be undefined, leading to Math.PI * r * r = number * undefined = NaN

and if you never used this

var theArea=calculateArea(radius);

then the function won't be executed automatically

about 4 years ago · Juan Pablo Isaza Denunciar

0

No, defining a function doesn't run it automatically. (After all, if it requires an argument, like r in your case, what would that r be?)

You will need to call the function (calculateArea(1)) for it to be run, but you don't need to assign the return value to a variable (area = calculateArea(1)) for that to happen.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda